package com.fengye.rabbit.order.rabbit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fengye.rabbit.order.domain.UserOrder;
import com.fengye.rabbit.order.dto.MessageDTO;
import com.fengye.rabbit.order.service.UserOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 *
 * @author sky
 * @date 2021年06月10日 2:15 下午
 */
@Component
@Slf4j
public class OrderConsumer {

    @Autowired
    UserOrderService userOrderService;

    @RabbitListener(queues = "${order.consumer.real.queue.name}", containerFactory = "singleListenerContainer")
    public void receiveMsg(@Payload MessageDTO dto){
        log.info("接收到消息，orderNo={}", dto.getOrderNo());

        try {
            // TODO: 更新订单的状态
            QueryWrapper<UserOrder> wrapper = new QueryWrapper<>();
            wrapper.eq("order_no",dto.getOrderNo()).eq("status", "1");
            UserOrder userOrder = userOrderService.getOne(wrapper);
            if (userOrder!=null) {
                userOrderService.updateUserOrderRecord(userOrder);
                log.info("更新 orderNo={} 的状态为失效", userOrder.getOrderNo());
            }else {
                log.info("订单编号 orderNo={} 不存在", dto.getOrderNo());
            }

        } catch (Exception e) {
            log.error("更新 orderNo={} 的状态失败", dto.getOrderNo());
        }
    }
}
